package com.tanhua.manage.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-log",consumerGroup = "tanhua-log-consumer")
public class LogMessageListener implements RocketMQListener<String> {

    @Autowired
    private LogService logService;

    @Override
    public void onMessage(String s) {
        //获取消息数据
        Map<String,Object> map = JSON.parseObject(s,Map.class);

        //userId默认解析为Integer类型，不能直接转换成Long，因此作如下处理
        Long userId = Long.valueOf(map.get("userId").toString());
        String type = (String) map.get("type");
        String date = (String) map.get("date");

        //创建日志对象，封装消息数据
        Log log = new Log();
        log.setUserId(userId);
        log.setType(type);
        log.setLogTime(date);
        log.setCreated(new Date());
        log.setUpdated(new Date());

        //保存日志
        logService.save(log);

    }
}
